package com.gopay.bis.spdb.ebank.operation;

import com.csii.payment.client.core.MerchantSignVerify;
import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.spdb.ebank.SpdbMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.ICertHelper;

/**
 * @SpdbB2cCertHelper.java
 * @author wanggang
 * @2013-12-5 下午1:41:32 www.gopay.com.cn Inc.All rights reserved.
 */
public class SpdbB2bCertHelper implements ICertHelper {

	@Override
	public String sign(BaseMessage baseMessage) throws GopayException {
		try {
			SpdbMessage message = (SpdbMessage) baseMessage;
			BankLogUtils.printB2BSignPlainLog(baseMessage);
			String signMsg = MerchantSignVerify.merchantSignData_ABA((String) message.getPlainMessage());
			BankLogUtils.printB2BSignSignatureLog(baseMessage, signMsg);
			return signMsg;
		} catch (Exception e) {
			BankLogUtils.printB2BSignExceptionLog(baseMessage, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6028);
		}
	}

	@Override
	public Object verify(BaseMessage baseMessage) throws GopayException {
		try {
			SpdbMessage message = (SpdbMessage) baseMessage;
			boolean isVeriyPass = false;
			BankLogUtils.printB2BVerifyPlainLog(baseMessage);
			BankLogUtils.printB2BVerifySignatureLog(baseMessage);
			isVeriyPass = MerchantSignVerify.merchantVerifyPayGate_ABA(message.getSignMessage(), message.getPlainMessage());
			BankLogUtils.printB2BVerifyResultLog(baseMessage, isVeriyPass);
			return isVeriyPass;
		} catch (Exception e) {
			BankLogUtils.printB2BVerifyExceptionLog(baseMessage, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6030);
		}
	}

}
